package webmanage

import (
	"context"
	"fmt"
	"github.com/go-kratos/kratos/v2/log"
	"phm/auth"
	webmanage2 "phm/internal/biz/webmanage"
	"phm/models/generator/model"
	"phm/tools"

	pb "phm/api/phm/v1/webmanage"
)

type UserService struct {
	pb.UnimplementedUserServer
	uc  *webmanage2.UserUseCase
	log *log.Helper
}

func NewUserService(uc *webmanage2.UserUseCase, logger log.Logger) *UserService {
	return &UserService{uc: uc, log: log.NewHelper(logger)}
}

const DefaultPassword = "123456"

func (s *UserService) DeleteStudents(ctx context.Context, req *pb.DeleteStudentsReq) (*pb.DeleteStudentsRep, error) {
	return &pb.DeleteStudentsRep{}, nil
}
func (s *UserService) DeleteUsers(ctx context.Context, req *pb.IdsReq) (*pb.Result, error) {
	return &pb.Result{}, nil
}
func (s *UserService) ExportStudent(ctx context.Context, req *pb.ExportStudentReq) (*pb.ExportStudentRep, error) {
	return &pb.ExportStudentRep{}, nil
}
func (s *UserService) ImportUserStatus(ctx context.Context, req *pb.Req) (*pb.Result, error) {
	return &pb.Result{}, nil
}
func (s *UserService) BatchImportUsers(ctx context.Context, req *pb.BatchImportUserReq) (*pb.BatchImportUserRep, error) {
	return &pb.BatchImportUserRep{}, nil
}
func (s *UserService) BatchSyncStudent(ctx context.Context, req *pb.BatchSyncStudentReq) (*pb.BatchSyncStudentRep, error) {
	return &pb.BatchSyncStudentRep{}, nil
}
func (s *UserService) SysMessage(ctx context.Context, req *pb.QuerySysMessageReq) (*pb.QuerySysMessageRep, error) {
	return &pb.QuerySysMessageRep{}, nil
}
func (s *UserService) SysMessageCount(ctx context.Context, req *pb.Req) (*pb.SysMessageCountRep, error) {
	return &pb.SysMessageCountRep{
		UnreadCount: 10,
	}, nil
}
func (s *UserService) SysMessageAllRead(ctx context.Context, req *pb.Req) (*pb.Result, error) {
	return &pb.Result{}, nil
}
func (s *UserService) SysMessageSingleRead(ctx context.Context, req *pb.IdReq) (*pb.Result, error) {
	return &pb.Result{}, nil
}
func (s *UserService) QueryUserList(ctx context.Context, req *pb.QueryUserRequest) (*pb.QueryUserReply, error) {
	return s.uc.GetUserList(ctx, req.Username, req.PageNum, req.PageSize)
}
func (s *UserService) LoginUser(ctx context.Context, req *pb.LoginUserRequest) (*pb.LoginUserReply, error) {
	userReply, err := s.uc.LoginUser(ctx, req.Username, req.Password)
	if err != nil {
		return nil, err
	}
	return userReply, nil
}
func (s *UserService) LogoutUser(ctx context.Context, req *pb.LogoutUserRequest) (*pb.LogoutUserReply, error) {
	return &pb.LogoutUserReply{}, nil
}
func (s *UserService) CreateUser(ctx context.Context, req *pb.CreateUserRequest) (*pb.CreateUserReply, error) {
	//TODO username 必须唯一
	nowUserID := fmt.Sprintf("%v", ctx.Value(auth.UserName))
	if req.Username == "" {
		req.Username = tools.RandStringBytesMaskImprSrcUnsafe(10)
	}
	if req.Password == "" {
		req.Password = tools.Encode(DefaultPassword)
	}
	pwd := tools.Encode(req.Password)
	token, err := s.uc.CreateUser(ctx, &model.SysUser{
		ID:             tools.GetUUID(),
		Creator:        &nowUserID,
		Username:       &req.Username,
		Password:       &pwd,
		RealName:       &req.RealName,
		Mobile:         &req.Mobile,
		Mailbox:        &req.Mailbox,
		JobName:        &req.JobName,
		CertificatesID: &req.CertificatesId,
		UserType:       &req.UserType,
		Remarks:        &req.Remarks,
	})
	if err != nil {
		return nil, err
	}
	return &pb.CreateUserReply{Token: token}, nil
}
func (s *UserService) UpdateUser(ctx context.Context, req *pb.UpdateUserRequest) (*pb.UpdateUserReply, error) {
	return &pb.UpdateUserReply{}, nil
}
func (s *UserService) DeleteUser(ctx context.Context, req *pb.DeleteUserRequest) (*pb.DeleteUserReply, error) {
	return &pb.DeleteUserReply{}, nil
}

func (s *UserService) DeleteUserByIds(ctx context.Context, req *pb.IdsReq) (*pb.DeleteUserReply, error) {
	return &pb.DeleteUserReply{}, nil
}
func (s *UserService) ResetUser(ctx context.Context, req *pb.ResetUserRequest) (*pb.ResetUserReply, error) {
	return &pb.ResetUserReply{}, nil
}
func (s *UserService) User2Role(ctx context.Context, req *pb.User2RoleRequest) (*pb.User2RoleReply, error) {
	return &pb.User2RoleReply{}, nil
}
func (s *UserService) UpdateUserPassword(ctx context.Context, req *pb.UpdateUserPasswordRequest) (*pb.UpdateUserPasswordReply, error) {
	return &pb.UpdateUserPasswordReply{}, nil
}
func (s *UserService) GetNowUserInfo(ctx context.Context, req *pb.Req) (*pb.LoginUserReply, error) {
	reply, err := s.uc.GetUserInfo(ctx, fmt.Sprintf("%v", ctx.Value(auth.UserId)))
	if err != nil {
		return nil, err
	}
	return reply, nil
}
func (s *UserService) GetNowUserRoles(ctx context.Context, req *pb.IdReq) (*pb.UserRolesRep, error) {
	return &pb.UserRolesRep{}, nil
}
func (s *UserService) VerificationUser(ctx context.Context, req *pb.VerificationUserReq) (*pb.Result, error) {
	return &pb.Result{}, nil
}
